package pers.zhuli.weblog.controller;

import org.springframework.web.bind.annotation.*;
import pers.zhuli.weblog.service.ArticleService;
import pers.zhuli.weblog.utils.Result;
import pers.zhuli.weblog.utils.ResultCode;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;

@RestController
@RequestMapping("/api/")
@CrossOrigin
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @PostMapping("article/{id}")
    public Result getArticle(@PathVariable String id) {
        return Result.success(articleService.getArticle(Integer.parseInt(id)));
    }

    @PostMapping("add_article")
    public Result addArticle(@RequestBody String json) {
        switch (articleService.addArticle(json)) {
            case 1:
                return Result.success();
            case 0:
                return Result.build(ResultCode.LOGIN_FAIL, "not login");
            case -1:
                return Result.build(ResultCode.SERVER_ERROR, "server_error");
        }
        return null;
    }
}
